using System;
using System.Collections;
using System.Diagnostics;
using UnityEngine;

[AddComponentMenu("2D Toolkit/UI/Core/tk2dUIItem")]
public class tk2dUIItem : MonoBehaviour
{
	//[DebuggerBrowsable(DebuggerBrowsableState.Never)]
	public event Action OnDown;

	//[DebuggerBrowsable(DebuggerBrowsableState.Never)]
	public event Action OnUp;

	//[DebuggerBrowsable(DebuggerBrowsableState.Never)]
	public event Action OnClick;

	//[DebuggerBrowsable(DebuggerBrowsableState.Never)]
	public event Action OnRelease;

	//[DebuggerBrowsable(DebuggerBrowsableState.Never)]
	public event Action OnHoverOver;

	//[DebuggerBrowsable(DebuggerBrowsableState.Never)]
	public event Action OnHoverOut;

	//[DebuggerBrowsable(DebuggerBrowsableState.Never)]
	public event Action<tk2dUIItem> OnDownUIItem;

	//[DebuggerBrowsable(DebuggerBrowsableState.Never)]
	public event Action<tk2dUIItem> OnUpUIItem;

	//[DebuggerBrowsable(DebuggerBrowsableState.Never)]
	public event Action<tk2dUIItem> OnClickUIItem;

	//[DebuggerBrowsable(DebuggerBrowsableState.Never)]
	public event Action<tk2dUIItem> OnReleaseUIItem;

	//[DebuggerBrowsable(DebuggerBrowsableState.Never)]
	public event Action<tk2dUIItem> OnHoverOverUIItem;

	//[DebuggerBrowsable(DebuggerBrowsableState.Never)]
	public event Action<tk2dUIItem> OnHoverOutUIItem;

	public static bool IsExcuteButton { get; private set; }

	private BoxCollider boxCollider
	{
		get
		{
			if (this._boxcollider == null)
			{
				this._boxcollider = (base.GetComponent<Collider>() as BoxCollider);
			}
			return this._boxcollider;
		}
	}

	public Vector3 downColliderSize
	{
		get
		{
			return this.colliderDownSize;
		}
	}

	private void Awake()
	{
		if (this.isChildOfAnotherUIItem)
		{
			this.UpdateParent();
		}
		if (this.boxCollider != null)
		{
			this.colliderSize = this.boxCollider.size;
			this.colliderCenter = this.boxCollider.center;
		}
	}

	private void Start()
	{
		if (tk2dUIManager.Instance == null)
		{
			UnityEngine.Debug.LogError("Unable to find tk2dUIManager. Please create a tk2dUIManager in the scene before proceeding.");
		}
		if (this.isChildOfAnotherUIItem && this.parentUIItem == null)
		{
			this.UpdateParent();
		}
	}

	public bool IsPressed
	{
		get
		{
			return this.isPressed;
		}
		set
		{
			this.isPressed = value;
		}
	}

	public tk2dUITouch Touch
	{
		get
		{
			return this.touch;
		}
	}

	public tk2dUIItem ParentUIItem
	{
		get
		{
			return this.parentUIItem;
		}
	}

	public void UpdateParent()
	{
		this.parentUIItem = this.GetParentUIItem();
	}

	public void ManuallySetParent(tk2dUIItem newParentUIItem)
	{
		this.parentUIItem = newParentUIItem;
	}

	public void RemoveParent()
	{
		this.parentUIItem = null;
	}

	public bool Press(tk2dUITouch touch)
	{
		return this.Press(touch, null);
	}

	public bool Press(tk2dUITouch touch, tk2dUIItem sentFromChild)
	{
		if (this.isPressed)
		{
			return false;
		}
		if (!this.isPressed)
		{
			this.touch = touch;
			if ((this.registerPressFromChildren || sentFromChild == null) && base.enabled)
			{
				this.isHoverOver = true;
				if (!this.isChildOfAnotherUIItem || this.isHoverEnabled)
				{
					this.DownUIItem(true);
				}
				else
				{
					this.isPressed = true;
					this.stationarySec = 0f;
				}
			}
			if (this.parentUIItem != null)
			{
				this.parentUIItem.Press(touch, this);
			}
		}
		return true;
	}

	public void UpdateTouch(tk2dUITouch touch)
	{
		if (base.enabled && this.isPressed && this.isChildOfAnotherUIItem && !this.isHoverEnabled && this.stationarySec < 0.05f)
		{
			if (touch.phase == TouchPhase.Stationary || (touch.phase == TouchPhase.Moved && touch.deltaPosition.magnitude * tk2dUIManager.PIXEL_TO_MILLIMETER < 1f))
			{
				this.stationarySec += tk2dUITime.deltaTime;
				if (this.stationarySec >= 0.05f)
				{
					this.isPressed = false;
					this.DownUIItem(true);
				}
			}
			else
			{
				this.stationarySec = 0f;
			}
		}
		this.touch = touch;
		if (this.parentUIItem != null)
		{
			this.parentUIItem.UpdateTouch(touch);
		}
	}

	private void DoSendMessage(string methodName)
	{
		if (this.sendMessageTarget != null && methodName.Length > 0 && !tk2dUIItem.IsExcuteButton)
		{
			TaskManager.StartCoroutine(this.CoSendMessage(methodName));
		}
	}

	private IEnumerator CoSendMessage(string methodName)
	{
		tk2dUIItem.IsExcuteButton = true;
		if (methodName.StartsWith("CoOn"))
		{
			yield return this.sendMessageTargetMonoBehaviour.StartCoroutine(methodName, this);
		}
		else
		{
			this.sendMessageTarget.SendMessage(methodName, this, SendMessageOptions.RequireReceiver);
		}
		tk2dUIItem.IsExcuteButton = false;
		yield break;
	}

	public void DownUIItem(bool updateCollider)
	{
		if (!this.isPressed)
		{
			this.isPressed = true;
			if (updateCollider)
			{
				this.BeginTracking();
			}
			if (this.OnDown != null)
			{
				this.OnDown();
			}
			if (this.OnDownUIItem != null)
			{
				this.OnDownUIItem(this);
			}
			this.DoSendMessage(this.SendMessageOnDownMethodName);
			if (this.parentUIItem != null)
			{
				this.parentUIItem.DownUIItem(updateCollider);
			}
		}
	}

	public void UpUIItem(bool updateCollider, tk2dUIItem upRoot = null)
	{
		if (this.isPressed)
		{
			this.isPressed = false;
			if (updateCollider)
			{
				this.EndTracking();
			}
			if (this.OnUp != null)
			{
				this.OnUp();
			}
			if (this.OnUpUIItem != null)
			{
				this.OnUpUIItem(this);
			}
			this.DoSendMessage(this.SendMessageOnUpMethodName);
			if (this.parentUIItem != null && upRoot != this.parentUIItem)
			{
				this.parentUIItem.UpUIItem(updateCollider, upRoot);
			}
		}
	}

	private void BeginTracking()
	{
		if (this.isHoverEnabled && this.boxCollider != null)
		{
			float buttonExpandThreshold = tk2dUIManager.buttonExpandThreshold;
			this.boxCollider.size += new Vector3(buttonExpandThreshold * 2f, buttonExpandThreshold * 2f, 0f);
			this.colliderDownSize = this.boxCollider.size;
			this.boxCollider.center -= new Vector3(0f, 0f, 0.1f);
		}
	}

	private void EndTracking()
	{
		if (this.isHoverEnabled && this.boxCollider != null)
		{
			this.boxCollider.center = this.colliderCenter;
			this.boxCollider.size = this.colliderSize;
			this.colliderDownSize = this.colliderSize;
		}
	}

	public void Release()
	{
		this.EndTracking();
		if (this.isPressed)
		{
			this.UpUIItem(false, null);
			if (this.OnClick != null)
			{
				this.OnClick();
			}
			if (this.OnClickUIItem != null)
			{
				this.OnClickUIItem(this);
			}
			this.DoSendMessage(this.SendMessageOnClickMethodName);
		}
		if (this.OnRelease != null)
		{
			this.OnRelease();
		}
		if (this.OnReleaseUIItem != null)
		{
			this.OnReleaseUIItem(this);
		}
		this.DoSendMessage(this.SendMessageOnReleaseMethodName);
		if (this.parentUIItem != null)
		{
			this.parentUIItem.Release();
		}
	}

	public tk2dUIItem CurrentOverUIItem(tk2dUIItem overUIItem)
	{
		tk2dUIItem tk2dUIItem = this;
		if (overUIItem != this)
		{
			if (this.isPressed)
			{
				if (!this.CheckIsUIItemChildOfMe(overUIItem))
				{
					tk2dUIItem = null;
					if (this.parentUIItem != null)
					{
						tk2dUIItem = this.parentUIItem.CurrentOverUIItem(overUIItem);
					}
					this.UpUIItem(false, tk2dUIItem);
				}
			}
			else if (this.parentUIItem != null)
			{
				tk2dUIItem = this.parentUIItem.CurrentOverUIItem(overUIItem);
			}
		}
		else if (!this.isPressed && this.isHoverEnabled)
		{
			this.DownUIItem(false);
		}
		if (!this.isHoverEnabled && tk2dUIItem != this)
		{
			return tk2dUIItem;
		}
		return this;
	}

	public bool CheckIsUIItemChildOfMe(tk2dUIItem uiItem)
	{
		tk2dUIItem tk2dUIItem = null;
		bool result = false;
		if (uiItem != null)
		{
			tk2dUIItem = uiItem.parentUIItem;
		}
		while (tk2dUIItem != null)
		{
			if (tk2dUIItem == this)
			{
				result = true;
				break;
			}
			tk2dUIItem = tk2dUIItem.parentUIItem;
		}
		return result;
	}

	public void Exit()
	{
		if (this.isPressed)
		{
			this.UpUIItem(false, null);
		}
	}

	public bool HoverOver(tk2dUIItem prevHover)
	{
		bool flag = false;
		tk2dUIItem tk2dUIItem = null;
		if (!this.isHoverOver)
		{
			this.DownUIItem(false);
			this.isHoverOver = true;
		}
		if (prevHover == this)
		{
			flag = true;
		}
		if (this.parentUIItem != null && this.parentUIItem.isHoverEnabled)
		{
			tk2dUIItem = this.parentUIItem;
		}
		if (tk2dUIItem == null)
		{
			return flag;
		}
		return tk2dUIItem.HoverOver(prevHover) || flag;
	}

	public void HoverOut(tk2dUIItem currHoverButton)
	{
		if (this.isHoverOver)
		{
			this.UpUIItem(false, null);
			this.isHoverOver = false;
		}
		if (this.parentUIItem != null && this.parentUIItem.isHoverEnabled)
		{
			if (currHoverButton == null)
			{
				this.parentUIItem.HoverOut(currHoverButton);
			}
			else if (!this.parentUIItem.CheckIsUIItemChildOfMe(currHoverButton) && currHoverButton != this.parentUIItem)
			{
				this.parentUIItem.HoverOut(currHoverButton);
			}
		}
	}

	private tk2dUIItem GetParentUIItem()
	{
		Transform parent = base.transform.parent;
		while (parent != null)
		{
			tk2dUIItem component = parent.GetComponent<tk2dUIItem>();
			if (component != null)
			{
				return component;
			}
			parent = parent.parent;
		}
		return null;
	}

	public void SimulateClick()
	{
		this.BeginTracking();
		if (this.OnDown != null)
		{
			this.OnDown();
		}
		if (this.OnDownUIItem != null)
		{
			this.OnDownUIItem(this);
		}
		this.DoSendMessage(this.SendMessageOnDownMethodName);
		this.EndTracking();
		if (this.OnUp != null)
		{
			this.OnUp();
		}
		if (this.OnUpUIItem != null)
		{
			this.OnUpUIItem(this);
		}
		this.DoSendMessage(this.SendMessageOnUpMethodName);
		if (this.OnClick != null)
		{
			this.OnClick();
		}
		if (this.OnClickUIItem != null)
		{
			this.OnClickUIItem(this);
		}
		this.DoSendMessage(this.SendMessageOnClickMethodName);
		if (this.OnRelease != null)
		{
			this.OnRelease();
		}
		if (this.OnReleaseUIItem != null)
		{
			this.OnReleaseUIItem(this);
		}
		this.DoSendMessage(this.SendMessageOnReleaseMethodName);
	}

	public void InternalSetIsChildOfAnotherUIItem(bool state)
	{
		this.isChildOfAnotherUIItem = state;
	}

	public bool InternalGetIsChildOfAnotherUIItem()
	{
		return this.isChildOfAnotherUIItem;
	}

	public void SetColliderSize(float rate)
	{
		if (!this.isHoverEnabled)
		{
			return;
		}
		if (this.boxCollider != null)
		{
			this.boxCollider.size = new Vector3(this.colliderDownSize.x * rate, this.colliderDownSize.y * rate, this.boxCollider.size.z);
		}
	}

	public void UpdateDefaultColliderSize()
	{
		if (this.boxCollider != null)
		{
			this.colliderSize = this.boxCollider.size;
			this.colliderCenter = this.boxCollider.center;
		}
	}

	public GameObject sendMessageTarget;

	public MonoBehaviour sendMessageTargetMonoBehaviour;

	public string SendMessageOnDownMethodName = string.Empty;

	public string SendMessageOnUpMethodName = string.Empty;

	public string SendMessageOnClickMethodName = string.Empty;

	public string SendMessageOnReleaseMethodName = string.Empty;

	[SerializeField]
	private bool isChildOfAnotherUIItem;

	public bool registerPressFromChildren;

	public bool isHoverEnabled = true;

	public Transform[] editorExtraBounds = new Transform[0];

	public Transform[] editorIgnoreBounds = new Transform[0];

	private bool isPressed;

	private bool isHoverOver;

	private tk2dUITouch touch;

	private tk2dUIItem parentUIItem;

	private BoxCollider _boxcollider;

	private Vector3 colliderCenter;

	private Vector3 colliderSize;

	private Vector3 colliderDownSize;

	private float stationarySec;

	private const float DELAYED_PRESS_THRESHOLD = 0.05f;
}
